package com.bbc.yuntun.common.converter;


import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.converter.Converter;

import javax.annotation.Nonnull;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * LocalDateTime转换器
 */
@Slf4j
public class LocalDateTimeConverter implements Converter<String, LocalDateTime> {

    @Override
    public LocalDateTime convert(@Nonnull String source) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = null;
        try {
            //2020-01-01 00:00:00
            switch (source.length()){
                case 10 -> source += " 00:00:00";
                case 13 -> source += ":00:00";
                case 16 -> source += ":00";
            }
            dateTime = LocalDateTime.parse(source, df);
        } catch (Exception e) {
            log.error("LocalDateTime转换失败：{}", e.getMessage());
        }
        return dateTime;
    }

}
